package com.bj.zspace.bytebuddy.bbtest;

import com.bj.zspace.bytebuddy.normal_test.Service;
import net.bytebuddy.ByteBuddy;
import net.bytebuddy.dynamic.DynamicType;
import net.bytebuddy.implementation.MethodDelegation;
import net.bytebuddy.matcher.ElementMatchers;

import java.io.File;

/**
 * 修改方法的入参:
 *  通过方法MethodDelegation指定委托类: 修改方法的入参
 **/
public class UpdateArgumentClass {

    public static void main(String[] args) throws Exception{

        String path = Thread.currentThread().getContextClassLoader().getResource("").getPath();
        DynamicType.Unloaded<Service> unloaded = new ByteBuddy()
            .subclass(Service.class).name("a.b.SubService")
            .method(ElementMatchers.named("selectUser").and(ElementMatchers.returns(String.class)))
            // 修改入参 withBinder得方法
            .intercept(MethodDelegation.withDefaultConfiguration().withBinders().to(UserManager.class))
            .make();

        unloaded.saveIn(new File(path));
    }

}
